package org.atovio.structure.graph;

import org.atovio.structure.adt.Graph;
import org.atovio.structure.list.SeqList;

/**
 * 抽象图类
 *
 * @author AtoVio, Winter Yuan
 * @version 1.0
 */
public abstract class AbstractGraph<T> implements Graph<T> {
    /*
    最大权值
     */
    protected static final int MAX_WEIGHT = 0x0000fffff;
    /*
    定点顺序表,存储图的顶点集合
     */
    protected SeqList<T> vertexList;

    /**
     * 构造空图，顶点数为0
     */
    public AbstractGraph() {
        this.vertexList = new SeqList<>();
    }

    @Override
    public String toString() {
        return "Vertex Collection:" + this.vertexList + "\n";
    }

    /**
     * @return The count of vertexs
     */
    @Override
    public int vertexCount() {
        return this.vertexList.size();
    }

    /**
     * @param i the index of vertex i
     * @return the value of vertex i
     */
    @Override
    public T get(int i) {
        return this.vertexList.get(i);
    }

    /**
     * org.atovio.structure.set the value of vertex i
     *
     * @param i     vertex's index
     * @param value the value
     */
    @Override
    public void set(int i, T value) {
        this.vertexList.set(i, value);
    }

    /**
     * remove the first vertex which is equal to the
     *
     * @param key the key
     * @return removed vertex
     */
    @Override
    public T remove(T key) {
        return this.remove(this.search(key));
    }

    /**
     * find the first vertex that is equal to the key and then return vertex's index
     *
     * @param key the key
     * @return the index of vertex
     */
    @Override
    public int search(T key) {
        return this.vertexList.search(key);
    }
}
